package com.acxy.ants.core.plugin.ehcache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import java.util.List;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-06-07
 */
public class CacheKit {

    public static CacheManager cacheManager;

    /**
     * 放入缓存值
     *
     * @param cacheName 缓存器名称
     * @param key       键
     * @param value     值
     */
    public static void put(String cacheName, String key, Object value) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
    }

    /**
     * 根据键获取值
     *
     * @param cacheName 缓存器名称
     * @param key       键
     * @return
     */
    public static Object get(String cacheName, String key) {
        Cache cache = cacheManager.getCache(cacheName);
        Element element = cache.get(key);
        return element == null ? null : element.getObjectValue();
    }


    /**
     * 根据键移除掉值
     *
     * @param cacheName 缓存器名称
     * @param key       键
     */
    public static void remove(String cacheName, String key) {
        Cache cache = cacheManager.getCache(cacheName);
        cache.remove(key);
    }

    /**
     * 得到缓存器对象
     *
     * @param cacheName 缓存名称
     * @return
     */
    public static Cache get(String cacheName) {
        return cacheManager.getCache(cacheName);
    }

    /**
     * 得到缓存器所有键
     *
     * @param cacheName 缓存名称
     * @return
     */
    public static List<String> getKeys(String cacheName) {
        Cache cache = cacheManager.getCache(cacheName);
        return cache.getKeys();
    }
}
